<template>
    <div class="starter" />
</template>

<script>
import Cookies from 'js-cookie';
import { StarterSetup } from '@/setup/starter/StarterSetup';
import { LoginSetup } from '@/setup/login/LoginSetup';
import { MqttConfig } from '@/mqtt.config';
import Mqtt from 'mqtt';
export default {
    name: 'Starter',
    data () {
        return {
            current_route_path: '/',
            login_route_path: '/login'
        };
    },
    setup () {
        const { getAndSaveDeviceParamFormatData, getUserRolesFromCookies, checkLogin, checkLoginByUsername } = StarterSetup();
        const { getLoignFrom } = LoginSetup();
        return { getAndSaveDeviceParamFormatData, getLoignFrom, getUserRolesFromCookies, checkLogin, checkLoginByUsername };
    },
    created () {
        this.start();
    },
    methods: {
        async start () {
            this.loadContentConfig();
            await this.checkLoginAndSetUserInfo();
            if (this.$store.state.islogin) {
                this.getUserRolesFromCookies();
                this.getAndSaveDeviceParamFormatData();
                this.getRepairTypes();
                this.getSolutionMode();
                this.getServiceMode();
                this.getSystemMessagePreview();
                this.reconnectMqtt();
            }
            this.$store.commit('SET_IS_INIT', true);
            const origin_route_path = this.getCurrentRoutePath();
            this.$router.push(origin_route_path);
        },
        getSystemMessagePreview  () {
            this.$store.dispatch('getSystemMessagePreview');
        },

        getCurrentRoutePath () {
            let fullpath = this.$route.query.origin;
            fullpath = (fullpath === '/' || fullpath === undefined || fullpath.indexOf('/starter') >= 0) ? this.$store.state.indexpage : fullpath;
            return fullpath;
        },
        getRepairTypes () {
            this.$store.dispatch('getSystemRepairType');
        },
        getSolutionMode () {
            this.$store.dispatch('getSystemSolutionMode');
        },
        getServiceMode () {
            this.$store.dispatch('getSystemServiceMode');
        },
        async  checkLoginAndSetUserInfo () {
            try {
                const openid = this.$route.query.openid || Cookies.get('openid');
                const username = Cookies.get('username');
                const loginfrom = this.getLoignFrom();

                if (loginfrom !== 'browser' && !openid) {
                    throw new Error('NO_OPENID');
                }
                if (loginfrom === 'browser') {
                    if (!username) {
                        this.$store.commit('SET_IS_LOGIN', false);
                        return false;
                    }
                    await this.checkLoginByUsername(username, loginfrom);
                } else {
                    await this.checkLogin(openid, loginfrom);
                }
                this.$store.commit('SET_IS_LOGIN', true);
                return true;
            } catch (error) {
                this.$store.commit('SET_IS_LOGIN', false);
                return false;
            }
        },
        loadContentConfig () {
            this.$store.dispatch('loadContentConfig',
                '/static/webconfig/config.content.json');
        },
        reconnectMqtt () {
            const { URL, OPTIONS } = MqttConfig();
            this.$setMqttConnection(Mqtt.connect(URL, OPTIONS));
        }

    }
};
</script>
